<template>
  <el-dialog
    title="修改"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <!--注意@keyup.enter.native是一个回车触发，就是把回车看成是提交-->
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item label="商品名称" prop="skuName">
        <el-input v-model="dataForm.skuName" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品介绍描述" prop="skuDesc">
        <el-input
          v-model="dataForm.skuDesc"
          placeholder="商品介绍描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品标题" prop="skuTitle">
        <el-input v-model="dataForm.skuTitle" placeholder="商品标题"></el-input>
      </el-form-item>
      <el-form-item label="商品副标题" prop="skuSubtitle">
        <el-input v-model="dataForm.skuSubtitle" placeholder="商品副标题"></el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="商品价格"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dataFormCancel()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// 导入已经集成好的
export default {
  components: {},
  data() {
    return {
      visible: false,
      dataForm: {
        skuId: -1,
        skuName: "",
        skuDesc: "",
        skuTitle: "",
        skuSubtitle: 1,
        price: "",
      },
      dataRule: {
        skuName: [{ required: true, message: "商品名称不能为空", trigger: "blur" }],
        skuTitle: [
          { required: true, message: "商品标题不能为空", trigger: "blur" },
        ],
        price: [
          { required: true, message: "商品价格不能为空", trigger: "blur" },
        ],
        showStatus: [
          {
            required: true,
            message: "显示状态[0-不显示；1-显示]不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.skuId = id || -1;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.skuId) {
          this.$http({
            url: this.$http.adornUrl(
              `/gulimallproduct/skuinfo/info/${this.dataForm.skuId}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.skuName = data.skuInfo.skuName;
              this.dataForm.skuDesc = data.skuInfo.skuDesc;
              this.dataForm.skuTitle = data.skuInfo.skuTitle;
              this.dataForm.skuSubtitle = data.skuInfo.skuSubtitle;
              this.dataForm.price = data.skuInfo.price;
            }
          });
        }
      });
    },
    // 关闭表单
    // 只有add情况下关闭表单才需要删除OSS中对应的文件
    // 这样防止在update关闭表单，却删除了OSS文件
    dataFormCancel() {
      this.visible = false;
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/gulimallproduct/skuinfo/info/webupdate`
            ),
            method: "post",
            data: this.$http.adornData({
              skuId: this.dataForm.skuId,
              skuName: this.dataForm.skuName,
              skuDesc: this.dataForm.skuDesc,
              skuTitle: this.dataForm.skuTitle,
              skuSubtitle: this.dataForm.skuSubtitle,
              price: this.dataForm.price,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>